//
//  ICMessageVController+Delegate.m
//  ICome
//
//  Created by Administrator on 2016/10/25.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICMessageVController+Delegate.h"
#import "ICMessageTableViewCell.h"
#import "ICCommonGroup.h"
#import "ICShareInstance.h"
#import "ICPullComboBoxView.h"
#import "ICGroupSearchController.h"
#import "ICChnController.h"


#import "IComMediator+ChatModuleActions.h"

@implementation ICMessageVController (Delegate)

#pragma mark - tableViewDelegate

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICMessageTableViewCell *cell = [ICMessageTableViewCell cellWithTableView:tableView];
    if (indexPath.row == self.dataArray.count - 1 ) {
        [cell setBottomLineStyle:CellLineStyleNone];
    }
    else {
        [cell setBottomLineStyle:CellLineStyleDefault];
    }
    cell.group = self.dataArray[indexPath.row];
    return cell;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *topTitle ,*readTitle;
    ICGroup *group  = self.dataArray[indexPath.row];
    topTitle  = group.isTop ? @"取消置顶" : @"置顶";
    readTitle = @"标记未读";
    if (group.unReadCount > 0) {
        readTitle = @"标记已读";
    }
//    readTitle = group.unReadCount ? @"标记未读" : @"标记已读";
    WEAKSELF
    //设置删除按钮
    UITableViewRowAction * deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [weakSelf deleteLocalGroup:indexPath];
        [weakSelf isRedCircleAppear];
    }];
    //置顶
    UITableViewRowAction * topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:topTitle handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [weakSelf setTopCellWithIndexPath:indexPath currentTop:group.isTop];
    }];
    //标记已读
    UITableViewRowAction * collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:readTitle handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [weakSelf markerReadWithIndexPath:indexPath currentUnReadCount:group.unReadCount];
        [weakSelf isRedCircleAppear];
    }];
    collectRowAction.backgroundColor = [UIColor lightGrayColor];
    topRowAction.backgroundColor     = [UIColor orangeColor];
    int gType = [group.gType intValue];
    if (gType == ICGroup_NOTIFY ||
        gType == ICGroup_TODO ||
        gType == ICGroup_CHANCE ||
        gType == ICGroup_ROBOT ||
        gType == ICGroup_SERVICE ||
        gType == ICGroup_DIRECTM) {
        return  @[deleteRowAction,collectRowAction];
    }
    return  @[deleteRowAction,collectRowAction,topRowAction];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ICGroup *group = self.dataArray[indexPath.row];
    // 客服
    NSDictionary *dict = @{@"group":group};
    if (group.gType.intValue == ICGroup_NOTIFY) {
        //通知
        UIViewController * viewController = [[IComMediator sharedInstance] IComMediator_NoticeViewController:dict];
        [self.navigationController pushViewController:viewController animated:YES];
    } else if(group.gType.intValue == ICGroup_CHANCE){
        UIViewController *viewController = [[IComMediator sharedInstance] IComMediator_ChnController:dict];
        [self.navigationController pushViewController:viewController animated:YES];
    } else if(group.gType.intValue == ICGroup_TODO){
        UIViewController * viewController = [[IComMediator sharedInstance] IComMediator_NoticeViewController:dict];
        [self.navigationController pushViewController:viewController animated:YES];
    } else if(group.gType.intValue == ICGroup_ROBOT){
        // 小助手
        UIViewController * chatVc = [[IComMediator sharedInstance] IComMediator_ChatViewController:dict];
        [self.navigationController pushViewController:chatVc animated:YES];
        [ICMessageHelper updateGroupReadWithGroupId:group.gId];
    } else if(group.gType.intValue == ICGroup_SERVICE){
        // 客服
        UIViewController * chatVc = [[IComMediator sharedInstance] IComMediator_ServeMessageViewController:dict];
        [self.navigationController pushViewController:chatVc animated:YES];
        
    } else if (group.gType.intValue == ICGroup_DIRECTM) {
        
//        UIViewController *viewController = [[IComMediator sharedInstance] IComMediator_DirectViewController:nil];
//        [self.navigationController pushViewController:viewController animated:YES];
        
    } else{
        //模型转字典
        UIViewController * viewController = [[IComMediator sharedInstance] IComMediator_ChatViewController:dict];
        [self.navigationController pushViewController:viewController animated:YES];
        [ICMessageHelper updateGroupReadWithGroupId:group.gId];
    }
}


#pragma mark - ICGroupSearchDelegate

- (void)selectRow:(ICGroup *)group
{
    ICChatViewController *chatVC = [[ICChatViewController alloc] init];
    chatVC.group = group;
    [self.navigationController pushViewController:chatVC animated:NO];
    // 移除searchBar
    if (self.searchController.active) {
        self.searchController.active = NO;
        [self.searchController.searchBar removeFromSuperview];
    }
}


#pragma mark - searchBarDelegate

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES];
    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            UIButton * cancel =(UIButton *)view;
            [cancel setTitle:@"取消" forState:UIControlStateNormal];
            [cancel setTitleColor:ICSEARCHCANCELCOLOR forState:UIControlStateNormal];
        }
    }
}

#pragma mark - ICPullComboBoxDelegate
- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ICCommonGroup *group = [self.boxView.dataList objectAtIndex:indexPath.section];
    ICCommonItem *model = [group.items objectAtIndex:indexPath.row];
    if (model.destVcClass) {
        ICBaseViewController *vc = [[model.destVcClass alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        if (model.option) {
            model.option();
        }
    }
}

#pragma mark - shareDelegate

- (void)shareViewController:(ICShareViewController *)shareVc confirmForShareData:(ICShareInstance *)shareData
{
    ICChatViewController *chatVc = [[ICChatViewController alloc] init];
    chatVc.group = shareData.group;
    chatVc.shareContent = shareData;
    [self.navigationController pushViewController:chatVc animated:NO];
    [shareVc dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UISearchResultsUpdating

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *text = searchController.searchBar.text;
    if (text.length > 0) {
        [self searchResultWithText:text];
    }
}


@end
